﻿#region Copyright information
// 
// Copyright © 2005-2013 Yongkee Cho. All rights reserved.
// 
// This code is a part of the Biological Object Library and governed under the terms of the
// GNU Lesser General  Public License (LGPL) version 2.1 which accompanies this distribution.
// For more information on the LGPL, please visit http://bol.codeplex.com/license.
// 
// - Filename: EdgeBase.cs
// - Author: Yongkee Cho
// - Email: yongkeecho@gmail.com
// - Date Created: 2013-01-24 4:34 PM
// - Last Modified: 2013-01-25 3:59 PM
// 
#endregion
using System;

namespace BOL.Algorithms.Graph
{
    /// <summary>Represents an edge that connects two vertices.</summary>
    /// <typeparam name="TVertex"></typeparam>
    public abstract class EdgeBase<TVertex> : IEdge<TVertex>
    {
		#region Public properties

        /// <summary>Gets or sets source vertex.</summary>
        public TVertex Source { get; private set; }

		/// <summary>Gets or sets target vertex.</summary>
        public TVertex Target { get; private set; }

		#endregion

		#region Constructor

		/// <summary>
		/// Initializes a new instance of the Edge class.
		/// </summary>
        protected EdgeBase(TVertex source, TVertex target)
		{
            Source = source;
            Target = target;
		}

		#endregion

        #region IEquatable<IEdge<TVertex>> implementation

        public bool Equals(IEdge<TVertex> other)
        {
            return Source.Equals(other.Source) && Target.Equals(other.Target);
        }

        #endregion

		#region Object override methods

        public override int GetHashCode()
        {
            return Source.GetHashCode() ^ Target.GetHashCode();
        }

        public override bool Equals(object other)
        {
            if (other == null)
                throw new ArgumentNullException();

            if (!(other is IEdge<TVertex>))
                throw new InvalidCastException("The 'other' argument is not an IEdge<TVertex> object.");

            return Equals(other as IEdge<TVertex>);
        }

		/// <summary>
		/// Returns a string that represents this edge. 
		/// </summary>
		public override string ToString()
		{
            return String.Format("{0}->{1}", Source, Target);
		}

		#endregion
	}
}
